﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace SYH.Public.BLL
{
    public class PublicAlbumImg
    {
        private static readonly DAL.PublicAlbumImg Dal = new DAL.PublicAlbumImg();
        private static readonly DAL.PublicAlbum Album = new DAL.PublicAlbum();
        #region 增
        /// <summary>
        /// 添加图片，对相册图片个数已+1
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static int Add(Model.PublicAlbumImg Model)
        {
            string oldExten = Path.GetExtension(Model.Name);

            if (!string.IsNullOrEmpty(oldExten) && !string.IsNullOrEmpty(Model.Name))
            {
                if (oldExten.ToLower() != Model.Extension.ToLower())
                {
                    Model.Name = Model.Name.Replace(oldExten, Model.Extension);
                }
            }
            int id = Dal.Add(Model);
            //album.RefeshAlumImageCount(Model.AlbumId);
            return id;
        }
        #endregion

        #region 查
        /// <summary>
        /// GetModel
        /// </summary>
        /// <param name="Id"></param>
        public static Model.PublicAlbumImg GetModel(int Id)
        {
            return Dal.GetModel(Id);
        }
        #endregion

        #region 删除多张图片 假删
        /// <summary>
        /// 删除多张图片 假删
        /// </summary>
        /// <param name="IdList">Id 列表  ,分隔</param>
        /// <param name="AlbumId">相册Id</param>
        /// <param name="Num">删除个数</param>
        public static void Delete(string IdList, int AlbumId, int Num)
        {
            Dal.Delete(IdList, AlbumId, Num);
            Album.RefeshAlumFontCover(AlbumId);
            Album.RefeshAlumImageCount(AlbumId);
        }
        #endregion

        #region 获取商家指定相册的图片列表
        /// <summary>
        /// 获取商家指定相册的图片列表
        /// </summary>
        /// <param name="albumId">相册编号</param>
        /// <param name="userId">用户编号</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="imageSize"></param>
        /// <param name="pageCount"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public static DataTable GetMyPic(int albumId, int userId, int pageIndex, int pageSize, SYH.Enum.ThumbImageSize imageSize, out int pageCount, out int recordCount, bool order)
        {
            string strCondition = string.Format(" and AlbumId={0} and UserId={1} and IsDelete=0", albumId, userId);

            return Common.PublicSqlData.SimplePager(Config.WebConfig.SqlConnString, "Public_AlbumImg", "Name,Id,Title,AlbumId", pageSize, pageIndex, "Id", order, strCondition, out pageCount, out recordCount).Tables[0];
        }
        #endregion

        #region 修改图片名
        public static int UpdateAlbumImgName(string id, string title)
        {
            return Dal.UpdateAlbumImgName(id, title);
        }
        #endregion

        #region 图片的批量移动
        /// <summary>
        /// 图片的批量移动
        /// </summary>
        /// <param name="Id">图片编号('1,2,3,4,5')</param>
        /// <param name="NewAlbumId">新相册编号</param>
        /// <returns></returns>
        public static bool MoveImage(string Id, int NewAlbumId, int oldeAlbumnId)
        {
            bool result = Dal.UpdateOneField("AlbumId", NewAlbumId.ToString(), " Id in (" + Id + ")", false);
            Album.RefeshAlumFontCover(oldeAlbumnId);
            Album.RefeshAlumImageCount(oldeAlbumnId);
            Album.RefeshAlumFontCover(NewAlbumId);
            Album.RefeshAlumImageCount(NewAlbumId);
            return result;
        }
        #endregion

        #region 修改图片详细
        public static int UpdateIngDetail(int id, string title, string summary)
        {
            return Dal.UpdateIngDetail(id, title, summary);
        }
        #endregion

        #region 获取图片标题
        /// <summary>
        /// 获取图片标题
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static string GetTitleById(int id)
        {
            return Dal.GetOneFieldValueById(id, "Title").ToString();
        }
        #endregion

        #region 根据条件获取相册列表
        /// <summary>
        /// 根据条件获取相册列表
        /// </summary>
        /// <param name="top"></param>
        /// <param name="strWhere"></param>
        /// <param name="filedOrder"></param>
        /// <returns></returns>
        public static IList<SYH.Public.Model.PublicAlbumImg> GetSimpleList(int top, string strWhere)
        {
            return Dal.GetSimpleList(top, strWhere);
        }
        #endregion
    }
}
